package com.KKd.controller;

import com.KKd.pojo.Blog;
import com.KKd.pojo.Comment;
import com.KKd.pojo.builder.CommentBuilder;
import com.KKd.pojo.dto.NoteDTO;
import com.KKd.service.BlogServiceImpl;
import com.KKd.service.CommentService;
import com.KKd.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 14587
 * @date 2020/11/13 15:49
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogServiceImpl blogService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CommentBuilder commentBuilder;
    //访问博客管理页面
    //1.首先要访问listBlog方法来展示所有的博客信息
    @RequestMapping("/toBlogAdmin")
    public String toBlogAdmin(){
        return "forward:/blog/listBlogManager";
    }
    @RequestMapping("/listBlogManager")
    public String listBlogManager(Model model){
        /*HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title", title);
        //这里两个名字用一个，从而让前端能够访问的到
        if (title!=null){
            Blog blog = blogService.queryBlog(map);
            model.addAttribute("blogs",blog);
        }*/
        //证明已经有地方查过了
        if (model.getAttribute("blogs")!=null){
            return "blogManager";
        }
        List<Blog> blogs = blogService.queryBlogExceptText();
        model.addAttribute("blogs",blogs);
        return "blogManager";
    }
//******************************************************************************
    //添加博客
    @RequestMapping("/toAddBlog")
    public String toAddBlog(){
        return "addUpdateBlog";
    }
    //删除博客
    @RequestMapping("/deleteBlog/{blogId}")
    public String deleteBlog(@PathVariable Integer blogId){
        blogService.deleteBlogById(blogId);
        return "redirect:/blog/toBlogAdmin";
    }
    //更新博客,跳转页面并回显信息
    @RequestMapping("/toUpdateBlog/{blogId}")
    public String toUpdateBlog(@PathVariable Integer blogId, Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("blogId", blogId);
        Blog blog = blogService.queryBlog(map);
        //到时候可以根据是否有Info来判断用不用写id
        model.addAttribute("updateBlogInfo",blog);
        return "addUpdateBlog";
    }
    //处理添加和更新博客的业务
    @RequestMapping("/addUpdateBlog")
    public String addUpdateBlog (Integer blogId,String title,String comment){
        //这里为了不多方法和跳转，直接处理情况，如果有id的话，一定不是0
        //下次传值的时候，如果是修改就传一个id，添加就直接传入0
        String date = JsonUtils.getJson(new Date());
        //修改
        if (blogId!=null){
            blogService.updateBlog(new Blog(blogId, title, date, 0, 0, comment));
        }
        //添加
        else {
            blogService.addBlog(new Blog(0, title, date,0,0,comment));
        }
        return "redirect:/blog/toBlogAdmin";
    }
//*****************************************************************************
    //博客管理中的ajax查找
    @RequestMapping("/searchAjax")
    @ResponseBody
    public List<String> searchAjax(String searchText, Model model){
        List<String> strings = blogService.queryBlogLike(searchText);
        return strings;
    }
    //博客管理中的查找
    @RequestMapping("/searchBlogAdmin")
    public String search(String title,Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", title);
        Blog blog = blogService.queryBlog(map);
        if (blog!=null){
            model.addAttribute("blogs",blog);
        }
        return "forward:/blog/listBlogManager";
    }
//*******************************************************************************
    //前往指定的博客页面
    @RequestMapping("/toBlog")
    public String toBlog(String title,Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        //这里是处理前端编程空格的参数
        if (title.contains(" ")){
            String replaceStr=title.replace(" ", "+");
            map.put("title", replaceStr);
        }else{
            map.put("title", title);
        }
        //map.put("title", title);
        //这里访问增加浏览量
        blogService.addBlogViewCount(title);
        //再查询要去的博客
        Blog blog = blogService.queryBlog(map);
        if (blog!=null){
            model.addAttribute(blog);
        }
        return "forward:/showCommBlog/"+ blog.getBlogId();
    }
//*******************************************************************************
    @RequestMapping("/showBlogs")
    public String showBlogIndex(Model model){
        //这里会出现问题，原因就是jacson不支持map的传输，会自动过滤掉里面的一些内容，所以导致有的值不能被搜索到
        List<Blog> blogs = blogService.queryBlogTitleContent();
        model.addAttribute("blogs",blogs);
        return "userIndex";
    }
//*******************************************************************************

    @RequestMapping("/addThumbBlog")
    @ResponseBody
    public int addThumbBlog(Integer blogId){
        blogService.addBlobThumbNum(blogId);
        int blogCounts = blogService.queryThumbs(blogId);
        return blogCounts;
    }

    //获取全部评论
    @RequestMapping("/comments/{blogId}")
    public List<Map<String, Object>> getNotesByIdeaId(
            HttpServletRequest request,
            @PathVariable("blogId") Integer blogId) {

        List<Comment> comments = commentService.queryComments(blogId);
        if (CollectionUtils.isEmpty(comments)) {
            return Collections.emptyList();
        }
        //TODO:更新notedto和comment的对应
        List<NoteDTO> noteDTOs = commentBuilder.buildNoteDTOs(comments);
        return commentBuilder.transform2Tree(noteDTOs);
    }
}
